#ifndef SYSTEM_H_
#define SYSTEM_H_

#include <map>
#include <vector>
#include <pthread.h>

#include "component.h"
#include "sound.h"
#include "sync.h"

#define MILLISEC 100000
#define UNBOUND 0

class Interval {
public:
	Interval(const float from, const float to);
	bool contains(const float t);
	float alpha(const float t);

	float from;
	float to;
};

bool operator< (const Interval& a, const float t);
bool operator< (const Interval& a, const Interval &b);
bool operator> (const Interval& a, const Interval &b);

class System {
public:
	System(Sync* sync);
	~System();

	void registerComponent(int id, Component* component);
	void activate(int id, Interval interval);

	void run(float run);
	void start();
	void stop();

	void preloaderThread();

protected:
	void startPreloader();

private:
	Sync* sync;
	Sound* sound;
	std::map<int, Component*> components;
	std::multimap<int, Interval> activation;

	bool preloadDone;
	pthread_t preloadThread;
};

#endif /* SYSTEM_H_ */
